﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data;
using System.Data.SqlClient;

namespace DAL
{
    public class BranchProvider:DataAccess
    {
        public BranchProvider()
        {
            this.CacheDuration = Globals.Settings.Branch.CacheDuration;
            this.EnableCaching = Globals.Settings.Branch.EnableCaching;
            this.ConnectionString = Globals.Settings.ConnectionString;
        }

        #region IDataReader --> Object
        protected virtual BranchsDetails GetBranchFromReader(IDataReader reader)
        {
            return new BranchsDetails(
                (int)reader["BranchID"],
                reader["Address"].ToString(),
                reader["Ward"].ToString(),
                reader["District"].ToString(),
                reader["Phone"].ToString(),
                reader["City"].ToString(),
                (bool)reader["IsHeadOffice"],
                reader["Name"].ToString(),
                reader["Number"].ToString());
        }

        protected virtual List<BranchsDetails> GetBranchCollectionFromReader(IDataReader reader)
        {
            List<BranchsDetails> branchs = new List<BranchsDetails>();
            while (reader.Read())
                branchs.Add(GetBranchFromReader(reader));
            return branchs;
        }

        #endregion

        #region Branch Method
        #region Branch
        public List<BranchsDetails> ListBranch()
        {
            using (SqlConnection cn = new SqlConnection(this.ConnectionString))
            {
                SqlCommand cmd = new SqlCommand("BranchsList", cn);
                cmd.CommandType = CommandType.StoredProcedure;
                cn.Open();
                return GetBranchCollectionFromReader(ExecuteReader(cmd));
            }
        }

        public BranchsDetails BranchGet(int branchID)
        {
            using (SqlConnection cn = new SqlConnection(this.ConnectionString))
            {
                SqlCommand cmd = new SqlCommand("BranchsGet", cn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@BranchID", SqlDbType.Int).Value = branchID;
                cn.Open();
                IDataReader reader = ExecuteReader(cmd, CommandBehavior.SingleRow);
                if (reader.Read())
                    return GetBranchFromReader(reader);
                else
                    return null;
            }
        }

        public BranchsDetails BranchGetIsHeadOffice()
        {
            using (SqlConnection cn = new SqlConnection(this.ConnectionString))
            {
                SqlCommand cmd = new SqlCommand("BranchsGetHeadOffice", cn);
                cmd.CommandType = CommandType.StoredProcedure;
                cn.Open();
                IDataReader reader = ExecuteReader(cmd, CommandBehavior.SingleRow);
                if (reader.Read())
                    return GetBranchFromReader(reader);
                else
                    return null;
            }
        }

        public int InsertBranch(BranchsDetails branch)
        {
            using (SqlConnection cn = new SqlConnection(this.ConnectionString))
            {
                SqlCommand cmd = new SqlCommand("BranchsInsert", cn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@Address", SqlDbType.NVarChar).Value = branch.Address;
                cmd.Parameters.Add("@Ward", SqlDbType.NVarChar).Value = branch.Ward;
                cmd.Parameters.Add("@District", SqlDbType.NVarChar).Value = branch.District;
                cmd.Parameters.Add("@Phone", SqlDbType.NVarChar).Value = branch.Phone;
                cmd.Parameters.Add("@City", SqlDbType.NVarChar).Value = branch.City;
                cmd.Parameters.Add("@IsHeadOffice", SqlDbType.Bit).Value = branch.IsHeadOffice;
                cmd.Parameters.Add("@Name", SqlDbType.NVarChar).Value = branch.Name;
                cmd.Parameters.Add("@Number", SqlDbType.NVarChar).Value = branch.Number;
                cmd.Parameters.Add("@BranchID", SqlDbType.Int).Direction = ParameterDirection.Output;
                cn.Open();
                int ret = ExecuteNonQuery(cmd);
                return (int)cmd.Parameters["@BranchID"].Value;
            }
        }

        public bool UpdateBranch(BranchsDetails branch)
        {
            using (SqlConnection cn = new SqlConnection(this.ConnectionString))
            {
                SqlCommand cmd = new SqlCommand("BranchsUpdate", cn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@Address", SqlDbType.NVarChar).Value = branch.Address;
                cmd.Parameters.Add("@Ward", SqlDbType.NVarChar).Value = branch.Ward;
                cmd.Parameters.Add("@District", SqlDbType.NVarChar).Value = branch.District;
                cmd.Parameters.Add("@Phone", SqlDbType.NVarChar).Value = branch.Phone;
                cmd.Parameters.Add("@City", SqlDbType.NVarChar).Value = branch.City;
                cmd.Parameters.Add("@IsHeadOffice", SqlDbType.Bit).Value = branch.IsHeadOffice;
                cmd.Parameters.Add("@Name", SqlDbType.NVarChar).Value = branch.Name;
                cmd.Parameters.Add("@Number", SqlDbType.NVarChar).Value = branch.Number;
                cmd.Parameters.Add("@BranchID", SqlDbType.Int).Value = branch.BranchID;
                cn.Open();
                int ret = ExecuteNonQuery(cmd);
                return (ret == 1);
            }
        }

        public bool DeleteBranch(int branchID)
        {
            using (SqlConnection cn = new SqlConnection(this.ConnectionString))
            {
                SqlCommand cmd = new SqlCommand("BranchsDelete", cn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@BranchID", SqlDbType.Int).Value = branchID;
                cn.Open();
                int ret = ExecuteNonQuery(cmd);
                return (ret == 1);
            }
        }

        #endregion

        #endregion
    }
}
